package com.example.b_308.myapplication.view.base;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.b_308.myapplication.R;
import com.example.b_308.myapplication.comment.ActivityTack;
import com.example.b_308.myapplication.comment.utils.AppUtils;
import com.example.b_308.myapplication.comment.utils.Global;
import com.example.b_308.myapplication.comment.view.title.TitleBar;

/**
 * Created by B-308 on 2017/11/15.
 */
public abstract class BaseActivity extends AppCompatActivity implements IBaseView {
    AlertDialog alertDialog;
    AlertDialog.Builder builder;
    LinearLayout ll;
    TitleBar titleBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        ActivityTack.getInstanse().addActivity(this);
        if (null != getSupportActionBar())
            getSupportActionBar().hide();
        Global.init(this);
        Global.setNoStatusBarFullMode(this);
        ll = (LinearLayout) findViewById(R.id.ll);
        titleBar = (TitleBar) findViewById(R.id.title_bar);
        builder = new AlertDialog.Builder(this);
//        titleBar.setPadding(0,(int) AppUtils.getStatusBarHeight(this),0,0);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
        view.setLayoutParams(lp);
        ll.addView(view);
    }

    public TitleBar getTitleBar() {
        return titleBar;
    }

    public void tentTo(Class<BaseActivity> cLass) {
        startActivity(new Intent(this, cLass));
    }

    public void tend2(Class cLass) {
        startActivity(new Intent(this, cLass));
    }

    public <T> void tentForResult(Class cLass, int requestCode) {
        Intent intent = new Intent(this, cLass);
        startActivityForResult(intent,requestCode);
    }

    public <T> void tent2(Class cLass, T t, String key) {
        Intent intent = new Intent(this, cLass);
        if (t instanceof String) {
            intent.putExtra(key, (String) t);
        }
    }

    public void fail(String message) {
        builder.setTitle("提示").setMessage(message).setPositiveButton("确定", null);
        alertDialog = builder.create();
        alertDialog.show();
    }

    @Override
    public void finish() {
        super.finish();
        ActivityTack.getInstanse().removeActivity(this);
    }

    public <T extends View> T findView(int resouceId) {
        return (T) findViewById(resouceId);
    }

}
